import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.script.Random;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSInterfaceChild;
import com.speljohan.rsbot.script.wrappers.RSNPC;

/**
 * @author Qauters Solves Sandwhich Lady Random AND Drizzt1112
 */
public class SandwhichLady extends Random {
	final static int ID_InterfaceSandwhichWindow = 297;
	final static int ID_InterfaceSandwhichWindowText = 48;
	final static int ID_InterfaceTalk = 243;
	final static int ID_InterfaceTalkText = 7;
	final static int[] ID_Items = { 10728, 10732, 10727, 10730, 10726, 45666,
			10731 };
	final static int ID_SandwhichLady = 8630;
	final static String[] Name_Items = { "chocolate", "triangle", "roll",
			"pie", "baguette", "doughnut", "square" };
	final boolean DEBUG = true; // Set to true for more info!

	/**
	 * This is done, because sandwhich lady tp's away lol
	 */
	private boolean solved = false;

	@Override
	public boolean activateCondition() {
		solved = false;
		final RSNPC lady = getNearestNPCByID(SandwhichLady.ID_SandwhichLady);

		if (lady == null) {
			return false;
		}

		if (lady != null) {
			return true;
		}

		return false;
	}

	@Override
	public String getAuthor() {
		return "Qauters & Drizzt1112";
	}

	@Override
	public String getName() {
		return "Sandwhich Lady";
	}

	@Override
	public double getVersion() {
		return 2;
	}

	@Override
	public int loop() {
		final RSInterface Chat1 = RSInterface.getInterface(243);
		if (Chat1.isValid()) {
			atInterface(243, 7);
			return random(900, 1200);
		}
		// Check if we finished it, and terminate random
		if (solved) {
			atObject(findObject(11373), "Enter");
			return -1;
		}

		// Check if we need to press continue, on the talk interface
		if (Bot.getClient().getValidInterfaceArray()[SandwhichLady.ID_InterfaceTalk]) {
			atInterface(SandwhichLady.ID_InterfaceTalk,
					SandwhichLady.ID_InterfaceTalkText);
			return random(900, 1200);
		}

		// Check if the sandwhich window is open
		if (Bot.getClient().getValidInterfaceArray()[SandwhichLady.ID_InterfaceSandwhichWindow]) {
			final RSInterface window = RSInterface
					.getInterface(SandwhichLady.ID_InterfaceSandwhichWindow);
			int offset = -1;
			final String txt = window.getChild(
					SandwhichLady.ID_InterfaceSandwhichWindowText).getText();
			for (int off = 0; off < SandwhichLady.Name_Items.length; off++) {
				if (txt.contains(SandwhichLady.Name_Items[off])) {
					offset = off;
					if (DEBUG) {
						log.info("Found: " + SandwhichLady.Name_Items[off]
								+ " - ID: " + SandwhichLady.ID_Items[off]);
					}
				}
			}
			for (int i = 7; i < 48; i++) {
				final RSInterfaceChild inf = window.getChild(i);

				if (DEBUG) {
					log.info("child[" + i + "] ID: " + inf.getModelID()
							+ " == " + SandwhichLady.ID_Items[offset]);
				}

				if (inf.getModelID() == SandwhichLady.ID_Items[offset]) {
					atInterface(inf);
					wait(random(900, 1200)); // Yea, use a wait here! (Wait's
					// are allowed in randoms.)

					if (!Bot.getClient().getValidInterfaceArray()[SandwhichLady.ID_InterfaceSandwhichWindow]) {
						log.info("Solved the Sandwich Lady, by eating a "
								+ SandwhichLady.Name_Items[offset]);
						solved = true;
						return random(6000, 7000);
					}
				}

			}
			return random(900, 1200);
		}
		final RSInterface Chat2 = RSInterface.getInterface(242);
		if (Chat2.isValid()) {
			atInterface(242, 6);
			solved = true;
			return random(900, 1200);
		}
		// Talk to the lady
		final RSNPC lady = getNearestNPCByID(SandwhichLady.ID_SandwhichLady);
		if (/* lady.isInteractingWithLocalPlayer() && */lady.getAnimation() == -1) { // TP
			// animation
			// maby?
			// We'll
			// see
			// lol
			clickCharacter(lady, "Talk");
		}
		return random(900, 1200);
	}

}